
import requests
import init_data
from dingtalkchatbot.chatbot import DingtalkChatbot

URLS = [line.rstrip('\n') for line in open('web-url')]

def message(text):

    at_mobiles = ['1732120xxxx']
    token = 'eb9f64xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx297e6b904920f4'
    ding_api = 'https://oapi.dingtalk.com/robot/send?access_token='+token

    send_message = DingtalkChatbot(ding_api)
    send_message.send_markdown(title='test', text=text, at_mobiles=at_mobiles)

def get_status(url):

    r = requests.get(url, allow_redirects=False)
    status = r.status_code

    failed_message = "### 网站状态异常 @173212xxxxx\n>" + url + "\n\n>" + str(status)

    if status != 200:
        message(failed_message)

    print(url, ':', status)

def main():
    for url in init_data.get_url('web_url'):
        get_status(url)


if __name__ == "__main__":
        main()
